package com.houdoudou.supplier.fragment;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.houdoudou.supplier.R;
import com.houdoudou.supplier.bean.Refund;
import com.houdoudou.supplier.bean.Order;
import com.houdoudou.supplier.bean.OrderProduct;
import com.houdoudou.supplier.bean.Supplier;
import com.houdoudou.supplier.common.AsynImageLoader;
import com.houdoudou.supplier.common.CommonFunction;
import com.houdoudou.supplier.common.Constant;
import com.houdoudou.supplier.common.ListViewUtil;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class OrderDetailFrg extends Fragment{

ListView productListView;
	
    ListAdapter productAdapter;
	com.houdoudou.supplier.common.AsynImageLoader imageLoader;
	TextView customer,address,timeAA;
	TextView moneyProduct,moneyFreight,moneyCu;
	TextView productnum,productVolume;
	EditText note;
	
	
	TextView orderID, orderStateWord;
	ListView orderStateListView;
	JSONArray orderStatesJA= new JSONArray();
	OrderStateListAdapter orderStateListAdapter;
	View.OnClickListener myClickListener;
	
	TextView  refundStateWord,refundResult;
	TextView orderMoney,refundMoney,refundType,refundReason;
	LinearLayout lt_detail;
	JSONArray refundStateJA= new JSONArray();
	ListView refundStateListView;
	RefundStateListAdapter refundStateListAdapter;
	
	
	
	private String AFFAIR_PROTECTORDER="protectOrder";
	private String AFFAIR_CANCELORDER="cancelOrder";
	private String TIME="time";
	private String AFFAIR="affair";
	private String NOTE="note";
	int orderState=-1;	
	int refundState=-1;
	JSONObject orderJO;
	JSONObject refundJO;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		 View view =inflater.inflate(R.layout.frg_orderdetail, container, false);
		 imageLoader= new AsynImageLoader(getActivity());
		 init_listener();
		 init_component(view);
		 assign_componenet();
		 return view;
	}
	
	@Override
	public void setArguments(Bundle bundle) {
			orderJO= JSONObject.fromObject(bundle.getString("order"));
			orderState =orderJO.getInt(Order.STATE);
	}

	private void init_listener(){
		
	}
	private void init_component(View view){
		productListView= (ListView) view.findViewById(R.id.listview_frg_orderdetail);
		
		moneyCu= (TextView)view.findViewById(R.id.tv_moneycu_frg_orderdetail);
		customer=(TextView)view.findViewById(R.id.tv_customertel_frg_orderdetail);
		address=(TextView)view.findViewById(R.id.tv_machineaddress_frg_orderdetail);
		timeAA=(TextView)view.findViewById(R.id.tv_arrivetime_frg_orderdetail);
		moneyProduct=(TextView)view.findViewById(R.id.tv_moneyproduct_frg_orderdetail);
		moneyFreight=(TextView)view.findViewById(R.id.tv_moneyfreight_frg_orderdetail);
		productVolume=(TextView)view.findViewById(R.id.tv_productvolume_frg_orderdetail);
		productnum=(TextView)view.findViewById(R.id.tv_productnum_frg_orderdetail);
		note= (EditText)view.findViewById(R.id.et_note_frg_orderdetail);
		note.setFocusable(false);
		
		orderID= (TextView) view.findViewById(R.id.tv_orderid_frgorderstate);
		orderStateWord= (TextView) view.findViewById(R.id.tv_state_frgorderstate);
		orderStateListView= (ListView) view.findViewById(R.id.listview_frgorderstate);
		
		refundStateWord= (TextView) view.findViewById(R.id.tv_state_frgorderrefund);
		refundStateListView= (ListView) view.findViewById(R.id.listview_frgorderrefund);
		refundResult= (TextView) view.findViewById(R.id.tv_refundresult_frgorderrefund);
		orderMoney = (TextView) view.findViewById(R.id.tv_ordermoney_frgorderrefund);
		refundType=(TextView) view.findViewById(R.id.tv_refundtype_frgorderrefund);
		refundMoney= (TextView) view.findViewById(R.id.tv_refundmoney_frgorderrefund);
		refundReason=(TextView) view.findViewById(R.id.tv_refundreason_frgorderrefund);
		lt_detail= (LinearLayout) view.findViewById(R.id.lt_refunddetail_frgorderrefund);
		
		productAdapter= new ProductListAdapter(getActivity());
		productListView.setAdapter(productAdapter);
		ListViewUtil.setListViewHeightBasedOnChildren(productListView);	
		
		orderStateListAdapter=new OrderStateListAdapter(getActivity());
		orderStateListView.setAdapter(orderStateListAdapter);
		ListViewUtil.setListViewHeightBasedOnChildren(orderStateListView);	
		
		refundStateListAdapter=new RefundStateListAdapter(getActivity());
		refundStateListView.setAdapter(refundStateListAdapter);
		ListViewUtil.setListViewHeightBasedOnChildren(refundStateListView);
	}
	private void assign_componenet(){

		moneyCu.setText("￥"+orderJO.getDouble(Order.MONEYCU)); 
		moneyFreight.setText("￥"+orderJO.getDouble(Order.MONEYFREIGHT)); 
	    moneyProduct.setText("￥"+orderJO.getDouble(Order.MONEYPRODUCT)); 	   
	    customer.setText("收货电话："+orderJO.getString(Order.CUSTOMERTELNUM));
	    address.setText(orderJO.getString(Order.MACHINEADDRESS));
	    timeAA.setText("送达时间："+orderJO.getString(Order.DATETIMEAA));
	    productnum.setText(orderJO.getString(Order.PRODUCTNUM));
	    productVolume.setText(orderJO.getString(Order.VOLUME)+"升");
		note.setText(orderJO.getString(Order.NOTE));
		
		orderID.setText("订单号："+orderJO.getLong(Order.ID));
		int state=orderJO.getInt(Order.STATE);
		orderStateWord.setText("状态："+CommonFunction.spellOrderStateWord(state));
		
		figureOutOrderState();
		System.out.println("orderStatesJA.size()"+orderStatesJA.size());
		orderStateListAdapter.notifyDataSetChanged();
		ListViewUtil.setListViewHeightBasedOnChildren(orderStateListView);
		
		
		
		if(!orderJO.containsKey("refund")){
			refundStateWord.setText("无退款");		
			refundResult.setVisibility(View.GONE);	 
			lt_detail.setVisibility(View.GONE);	
		}else{
			refundJO=orderJO.getJSONObject("refund");
	        refundState=refundJO.getInt(Refund.STATE);

			refundStateWord.setText("状态："+CommonFunction.spellRefundStateWord(refundState));
	        refundResult.setText("结果："+CommonFunction.spellRefundResultWord(refundJO.getInt(Refund.RESULT)));
	        refundResult.setVisibility(View.VISIBLE);
	        lt_detail.setVisibility(View.VISIBLE);
	        orderMoney.setText("总金额："+orderJO.getDouble(Order.MONEYCU)+"   总数量："+orderJO.getInt(Order.PRODUCTNUM));
	        refundMoney.setText("退款金额："+ refundJO.getDouble(Refund.MONEY));
	        refundType.setText("退款原因："+CommonFunction.spellRefundTypeWord(refundJO.getInt(Refund.TYPE)) );
	        refundReason.setText(refundJO.getString(Refund.REASON));
		  
	        figureOutRefundState();
			System.out.println("refundStateJA.size()"+refundStateJA.size());
			refundStateListAdapter.notifyDataSetChanged();
			ListViewUtil.setListViewHeightBasedOnChildren(refundStateListView);
		}
		
		
	}
	
class ProductListAdapter extends BaseAdapter {
		   
		
	    private LayoutInflater layoutInflater;  
	    private Context context;  
	    private int parentPosition;
	        public ProductListAdapter(Context context) {
				
			this.context = context;
		    this.layoutInflater=LayoutInflater.from(context); 
		  
		}


	    final class Zujian{  
	         public TextView product,branch,price,num,volume; 
	       
	         public  ImageView img;
	      
	        }  
	          @Override
	       public int getCount() {
		// TODO Auto-generated method stub
		    return orderJO.getJSONArray("products").size();  
	        	 
	       }

	      @Override
	       public Object getItem(int position) {
		// TODO Auto-generated method stub
		       return orderJO.getJSONArray("products").getJSONObject(position);  	    	 
	          }

	    @Override
	     public long getItemId(int position) {
		// TODO Auto-generated method stub
		       return position; 
	       }

	               @Override
	    public View getView(int position, View  convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final int childID = position;   
		 Zujian zj=null;  
	        if(convertView==null){  
	            zj=new Zujian();  
	            //获得组件，实例化组件  
	            convertView=layoutInflater.inflate(R.layout.list_product_frgorderdetail, null);  
	            zj.img=(ImageView) convertView.findViewById(R.id.img_product_listchild_actplaceorder);
	            
	            zj.num=(TextView) convertView.findViewById(R.id.tv_branchnum_listproduct_frgorderdetail);
	            zj.branch=(TextView) convertView.findViewById(R.id.tv_branchname_listproduct_frgorderdetail);
	            zj.product=(TextView) convertView.findViewById(R.id.tv_productname_listproduct_frgorderdetail);
	            zj.price=(TextView) convertView.findViewById(R.id.tv_branchprice_listproduct_frgorderdetail);
	            zj.volume=(TextView) convertView.findViewById(R.id.tv_branchvolume_listproduct_frgorderdetail);
	            convertView.setTag(zj);  
	            System.out.println("position----"+position+"------inflated");
	        }else{   System.out.println("position----"+position+"-------not inflated");
	            zj=(Zujian)convertView.getTag();  
	        }  		        						  
	        //绑定数据  
	      JSONObject record=orderJO.getJSONArray("products").getJSONObject(position);
	        zj.num.setText(record.getInt(OrderProduct.BRANCHNUM)+"个");
	        zj.price.setText(record.getDouble(OrderProduct.BRANCHPRICE)+"元");
	        zj.volume.setText(record.getInt(OrderProduct.BRANCHVOLUME)+"升");
	        zj.branch.setText(record.getString(OrderProduct.BRANCHNAME));
	        zj.product.setText(record.getString(OrderProduct.PRODUCTNAME)); 
	        
	      imageLoader.showImageAsyn(zj.img, record.getString(OrderProduct.DIR), R.drawable.p3);       
	        return convertView;
	       } 
	         }// end Adapter

class OrderStateListAdapter extends BaseAdapter {
	   
	
    private LayoutInflater layoutInflater;  
    private Context context;  
    private int parentPosition;
        public OrderStateListAdapter(Context context) {
			
		this.context = context;
	    this.layoutInflater=LayoutInflater.from(context); 
	  
	}


    final class Zujian{  
         public TextView time,affair; 
       
      
        }  
          @Override
       public int getCount() {
	// TODO Auto-generated method stub
	    return orderStatesJA.size();  
        	 
       }

      @Override
       public Object getItem(int position) {
	// TODO Auto-generated method stub
	       return orderStatesJA.getJSONObject(position);  
    	 
          }

    @Override
     public long getItemId(int position) {
	// TODO Auto-generated method stub
	       return position; 
       }

               @Override
    public View getView(int position, View  convertView, ViewGroup parent) {
	// TODO Auto-generated method stub
	final int childID = position;   
	 Zujian zj=null;  
        if(convertView==null){  
            zj=new Zujian();  
            //获得组件，实例化组件  
            convertView=layoutInflater.inflate(R.layout.list_orderstate, null);              
            zj.time=(TextView) convertView.findViewById(R.id.tv_time_list_frgorderstate);
            zj.affair=(TextView) convertView.findViewById(R.id.tv_affair_list_frgorderstate);
            convertView.setTag(zj);  
            System.out.println("position----"+position+"------inflated");
        }else{   System.out.println("position----"+position+"-------not inflated");
            zj=(Zujian)convertView.getTag();  
        }  		        						  
        //绑定数据  
        zj.time.setText(orderStatesJA.getJSONObject(childID).getString(TIME));
        zj.affair.setText(orderStatesJA.getJSONObject(childID).getString(AFFAIR));
       
        
   //     imageLoader.showImageAsyn(zj.img, record.getString(Product.DIR), R.drawable.p3);       
        return convertView;
       } 
         }// end Adapter
private void figureOutOrderState(){
	orderStatesJA.clear();
	 JSONObject stateJO= new JSONObject();stateJO.put(NOTE, "");
	if(orderState>=1){
		stateJO.put(TIME, orderJO.getString(Order.DATETIMEPL)); 
		stateJO.put(AFFAIR, "下单");  orderStatesJA.add(stateJO); }	
	if(orderState==3){
		stateJO.put(TIME, orderJO.getString(Order.DATETIMERF)); 
		stateJO.put(AFFAIR, "取消订单");  stateJO.put(NOTE, "过时未支付，自动取消");
		orderStatesJA.add(stateJO);
		return; }
	if(orderState==4){
		stateJO.put(TIME, orderJO.getString(Order.DATETIMERF)); 
		stateJO.put(AFFAIR, "取消订单");  stateJO.put(NOTE, "主动取消订单");
		orderStatesJA.add(stateJO); 
		return;}

	//orderState==10，意味着，已支付要送货的订单。
	
	if(orderJO.getInt(Order.ISMONEYPAID)==2){
		stateJO.put(TIME, orderJO.getString(Order.DATETIMEPA)); 
		stateJO.put(AFFAIR, "支付");   orderStatesJA.add(stateJO); }
	
	if(orderState==5){
		stateJO.put(TIME, orderJO.getString(Order.DATETIMERF)); 
		stateJO.put(AFFAIR, "取消订单");  stateJO.put(NOTE, "商家取消订单");
		orderStatesJA.add(stateJO); 
	
		 return; }	
	
	if(orderState>=21){
		stateJO.put(TIME, orderJO.getString(Order.DATETIMEAI)); 
		stateJO.put(AFFAIR, "送达");  stateJO.put(NOTE, "送货员："+orderJO.getString(Order.COURIERAITELNUM));
		orderStatesJA.add(stateJO); 
		}
	if(orderState==22){
		stateJO.put(TIME, orderJO.getString(Order.DATETIMETK)); 
		stateJO.put(AFFAIR, "收货"); stateJO.put(NOTE,"消费者取走订单");
		orderStatesJA.add(stateJO); }
	if(orderState==23){
		stateJO.put(TIME, orderJO.getString(Order.DATETIMERK)); 
		stateJO.put(AFFAIR, "取回"); stateJO.put(NOTE,"客户过时未取，商家取回，"+"送货员："+orderJO.getString(Order.COURIERRKTELNUM));
		orderStatesJA.add(stateJO); }
	if(orderState==24){
		stateJO.put(TIME, orderJO.getString(Order.DATETIMERK)); 
		stateJO.put(AFFAIR, "抛弃"); stateJO.put(NOTE,"客户过时未取，商家过时未取回，订单被抛弃，送货员："+orderJO.getString(Order.COURIERABTELNUM));
		orderStatesJA.add(stateJO); }
	
}

class RefundStateListAdapter extends BaseAdapter {
	   
	
    private LayoutInflater layoutInflater;  
    private Context context;  
    private int parentPosition;
        public RefundStateListAdapter(Context context) {
			
		this.context = context;
	    this.layoutInflater=LayoutInflater.from(context); 
	  
	}


    final class Zujian{  
         public TextView time,affair; 
       
      
        }  
          @Override
       public int getCount() {
	// TODO Auto-generated method stub
	    return refundStateJA.size();  
        	 
       }

      @Override
       public Object getItem(int position) {
	// TODO Auto-generated method stub
	       return refundStateJA.getJSONObject(position);  
    	 
          }

    @Override
     public long getItemId(int position) {
	// TODO Auto-generated method stub
	       return position; 
       }

               @Override
    public View getView(int position, View  convertView, ViewGroup parent) {
	// TODO Auto-generated method stub
	final int childID = position;   
	 Zujian zj=null;  
        if(convertView==null){  
            zj=new Zujian();  
            //获得组件，实例化组件  
            convertView=layoutInflater.inflate(R.layout.list_orderstate, null);              
            zj.time=(TextView) convertView.findViewById(R.id.tv_time_list_frgorderstate);
            zj.affair=(TextView) convertView.findViewById(R.id.tv_affair_list_frgorderstate);
            convertView.setTag(zj);  
            System.out.println("position----"+position+"------inflated");
        }else{   System.out.println("position----"+position+"-------not inflated");
            zj=(Zujian)convertView.getTag();  
        }  		        						  
        //绑定数据  
        zj.time.setText(refundStateJA.getJSONObject(childID).getString(TIME));
        zj.affair.setText(refundStateJA.getJSONObject(childID).getString(AFFAIR));
       
        
   //     imageLoader.showImageAsyn(zj.img, record.getString(Product.DIR), R.drawable.p3);       
        return convertView;
       } 
         }// end Adapter

private void figureOutRefundState(){
	refundStateJA.clear();
	
	 JSONObject stateJO= new JSONObject();stateJO.put(NOTE, "");
	if(refundState>=1){
		stateJO.put(TIME, refundJO.getString(Refund.DATETIMECP)); 
		stateJO.put(AFFAIR, "申请退款");  refundStateJA.add(stateJO); stateJO.put(NOTE, "");
		 }	
	
	if(refundState==2){  
		stateJO.put(TIME, refundJO.getString(Refund.DATETIMES)); 
		stateJO.put(AFFAIR, "商家同意退款");  stateJO.put(NOTE, "");
		refundStateJA.add(stateJO);
		return; }
	if(refundState==3){
		stateJO.put(TIME, refundJO.getString(Refund.DATETIMES)); 
		stateJO.put(AFFAIR, "退款成功");   stateJO.put(NOTE, "系统处理");
		refundStateJA.add(stateJO);
		return; }
	if(refundState>=4&&(!refundJO.getString(Refund.DATETIMES).isEmpty())){
		stateJO.put(TIME, refundJO.getString(Refund.DATETIMES)); 
		stateJO.put(AFFAIR, "商家拒绝退款");  stateJO.put(NOTE, "");
		refundStateJA.add(stateJO);
		 }
	if(refundState==5){
		stateJO.put(TIME, refundJO.getString(Refund.DATETIMEC)); 
		stateJO.put(AFFAIR, "退款失败");  stateJO.put(NOTE, "系统处理");
		refundStateJA.add(stateJO); 
		return;}
	if(refundState>=6&&(!refundJO.getString(Refund.DATETIMEC).isEmpty())){
		stateJO.put(TIME, refundJO.getString(Refund.DATETIMEC)); 
		stateJO.put(AFFAIR, "申请仲裁");  stateJO.put(NOTE, "");
		refundStateJA.add(stateJO); 
		 }
	if(refundState==7){
		stateJO.put(TIME, refundJO.getString(Refund.DATETIMEO)); 
		stateJO.put(AFFAIR, "仲裁同意退款");  stateJO.put(NOTE, "");
		refundStateJA.add(stateJO); 
		return; }
	if(refundState==8){
		stateJO.put(TIME, refundJO.getString(Refund.DATETIMEC)); 
		stateJO.put(AFFAIR, "仲裁拒绝退款");  stateJO.put(NOTE, "");
		refundStateJA.add(stateJO); 
		 return; }
	if(refundState==9){
		stateJO.put(TIME, refundJO.getString(Refund.DATETIMEC)); 
		stateJO.put(AFFAIR, "退款成功");   stateJO.put(NOTE, "系统处理");
		refundStateJA.add(stateJO); 
		return; }
	
	if(refundState==10){
		stateJO.put(TIME, refundJO.getString(Refund.DATETIMECC)); 
		stateJO.put(AFFAIR, "取消退款");   stateJO.put(NOTE, "");
		refundStateJA.add(stateJO); 
		return; }
	
}

}
